﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Core.Distribution;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.AppVersion;
using FwProject.Core.Car;
using FwProject.Car;
using FwProject.Core.FactoryInfo;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.TypeNumber;
using Castle.Core.Logging;
using Abp.ObjectMapping;
using Abp.ElasticSearch;
using FwProject.BrandInfo;
using System.Threading.Tasks;
using FwProject.BrandInfo.Dto;
using FwProject.ShoppingMall.Shop;
using FwProject.Shop.Dto;
using FwProject.Shop;
using FwProject.Keyword.Dto;
using FwProject.Keyword;
using System.Linq;
using Abp.Threading;
using FwProject.Comm;

namespace FwProject.ShopApplication.RemoteEventBus
{
    /// <summary>
    ///餐厅缓存数据
    /// </summary>
    [RemoteEventHandler(ForType = RemoteTypeHelp.TYPEDELETESEARCH, ForTopic = RemoteTypeHelp.TOPICAPPSYSTEMSEARCH)]
    public class RemoteEventBusDeleteSearch : IRemoteEventHandler, ITransientDependency
    {
        public ICacheManager _catchmanager { get; set; }
        public IElasticsearch _elasticsearch;
        public IBrandInfoAppService _brandInfoAppService;
        public IShopProductAppService _shopProductAppService;
        public IKeywordAppService _keywordAppService;
        public ILogger iLogger { get; set; }
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;

        public IObjectMapper _objectMapper { get; set; }
        public IShopTypeAppService _shopTypeAppService;
        public RemoteEventBusDeleteSearch(
                IElasticsearch elasticsearch,
                IBrandInfoAppService brandInfoAppService,
                IShopProductAppService shopProductAppService, IShopTypeAppService shopTypeAppService,
                IKeywordAppService keywordAppService,
                IRepository<ShopProduct, Guid> repositoryShopProduct
            )
        {
            _repositoryShopProduct = repositoryShopProduct;
            _keywordAppService =keywordAppService;
            _shopTypeAppService = shopTypeAppService;
            _objectMapper = NullObjectMapper.Instance;
            _elasticsearch = elasticsearch;
            _brandInfoAppService = brandInfoAppService;
            _shopProductAppService = shopProductAppService;
        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {

            iLogger.Info($"搜索引擎名称==={eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()}===搜索引擎处理");
            iLogger.Info($"搜索引擎名称==={eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString()}===搜索引擎处理");
            try
            {
                switch (eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString())
                {
                    case nameof(BrandDto):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<BrandDto, Guid>(nameof(BrandDto).ToLower(), nameof(BrandDto).ToLower(), _brandInfoAppService.GetByIdAsync(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        break;

                    case nameof(ShopProductDto):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<ShopProductDto, Guid>(nameof(ShopProductDto).ToLower(), nameof(ShopProductDto).ToLower(), _shopProductAppService.GetById(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        break;

                    case nameof(ShopProductAppDto):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<ShopProductAppDto, Guid>(nameof(ShopProductAppDto).ToLower(), nameof(ShopProductAppDto).ToLower(), _shopProductAppService.GetShopProductAppDto(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        break;

                    case nameof(ShopProductListAppDto):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<ShopProductListAppDto, Guid>(nameof(ShopProductListAppDto).ToLower(), nameof(ShopProductListAppDto).ToLower(), _shopProductAppService.GetListAppDto(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        break;

                    case nameof(ShopType):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<AppShopTypeDto, Guid>(nameof(AppShopTypeDto).ToLower(), nameof(AppShopTypeDto).ToLower(), _shopTypeAppService.GetSerachType(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        try
                        {
                            var model =AsyncHelper.RunSync<ShopTypeDto>(async ()=>await _shopTypeAppService.GetByIdAsync(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString())));
                            var list = _repositoryShopProduct.GetAll().Where(o => o.ShopTypeid == Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()));
                            foreach (var item in list)
                            {
                                if (model.Isactive)
                                    item.IsGrounding = true;
                                else
                                    item.IsGrounding = false;
                            }
                        }
                        catch (Exception exe)
                        {

                            iLogger.Error($"搜索引擎名称==={eventArgs.EventData.Data["id"].ToString()}===搜索引擎删除商品", exe);
                        }
                     


                        break;

                    case nameof(KeywordAppDto):
                        Task.Run(async () => await _elasticsearch.DeleteAsync<KeywordAppDto, Guid>(nameof(KeywordAppDto).ToLower(), nameof(KeywordAppDto).ToLower(), _keywordAppService.GetDeleteByIdAppKeyWord(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));
                        break;
                }

            }
            catch (Exception exe)
            {
                iLogger.Info($"搜索引擎名称==={eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString()}===搜索引擎生成错误", exe);
            }
        }
    }
}
